<?php

namespace app\admin\model;

use app\admin\service\Rbac;
use app\common\enums\StatusEnum;

class User extends \app\common\model\User
{
    public static function getList($where)
    {
        $sql = User::field('id, user_name, mobile, status, pure_password, last_login_time, create_time, update_time');
        if ($where['user_name']) {
            $sql->where('user_name', 'like', $where['user_name']);
        }

        if ($where['mobile']) {
            $sql->where('mobile', 'like', $where['mobile']);
        }

        if ($where['status'] != null && $where['status'] != -1) {
            $sql->where(['status' => $where['status']]);
        }

        $model = $sql->select()->toArray();

        $userIds = [];
        foreach ($model as $key => $value) {
            $userIds[$value['id']] = $value['id'];
        }

        // 管理员角色
        $role = [];
        if ($userIds) {
            $role = UserRole::getColumn($userIds);
        }

        foreach ($model as $key => $value) {
            $model[$key]['role'] = isset($role[$value['id']]) ? $role[$value['id']] : '暂无';
            $model[$key]['last_login_time'] = get_datetime($value['last_login_time']);
            $model[$key]['create_time'] = get_datetime($value['create_time']);
            $model[$key]['update_time'] = get_datetime($value['update_time']);
            $model[$key]['status'] = StatusEnum::getStatus($value['status']);
            // 角色搜索
            if ($where['role']) {
                if (strpos($model[$key]['role'], (string)$where['role']) === false) {
                    unset($model[$key]);
                }
            }
        }

        // 获取总记录数
        $count = $sql->count();
        return [
            'model' => $model,
            'count' => $count
        ];
    }

    /**
     * 删除管理员
     * @param $id
     */
    public static function deleteOne($id)
    {
        $model = static::get($id);
        if ($model) {
            $model->delete();
        }
    }

    public static function getOneByName($userName)
    {
        return static::field('id, user_name, password, salt, status, last_login_time')
            ->where(['user_name' => $userName])
            ->find();
    }

    /**
     * 添加管理员
     * @param $data
     * @return bool
     */
    public static function saveOne($data)
    {
        if ($data['password']) {
            $password = password($data['password']);
            $saveData = [
                'user_name' => $data['user_name'],
                'password' => $password['password'],
                'pure_password' => $data['password'],
                'salt' => $password['salt'],
                'mobile' => $data['mobile'],
                'status' => $data['status'],
            ];
        } else {
            $saveData = [
                'user_name' => $data['user_name'],
                'mobile' => $data['mobile'],
                'status' => $data['status'],
            ];
        }

        $where = [];
        if (isset($data['id']) && $data['id'] > 0) {
            $where = [
                'id' => $data['id']
            ];
        }

        $user = new User;
        // save方法第二个参数为更新条件
        return $user->save($saveData, $where);
    }

    /**
     * 分配角色
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function assignment($data)
    {
        $rbac = new Rbac();
        $res = $rbac->assignUserRole($data['user_id'], $data['role_id']);
        if ($res['code'] == 0) {
            $userId = $data['user_id'];
            // 重新缓存权限
            $rbac = new Rbac();
            $rbac->updatePermission($userId);
        }
        return $res;
    }

    
}